package main

import (
	"encoding/json"
	"fmt"
)

type Student struct {
	Name  string
	Age   int
	Score float32
}

type Classes struct {
	Title    string
	Students []*Student
}

type Teacher struct {
	Name string
	Age  int
	ID   int `json:"ID"`
}

func main() {
	c := &Classes{
		Title:    "",
		Students: make([]*Student, 0, 200),
	}
	for i := 0; i < 10; i++ {
		student := &Student{
			Name:  fmt.Sprintf("stu%02d", i),
			Age:   10,
			Score: 10,
		}
		c.Students = append(c.Students, student)
	}
	// JSON序列化：结构体-->JSON格式的字符串
	data, err := json.Marshal(c)
	if err != nil {
		fmt.Println("json marshal failed")
		return
	}
	fmt.Printf("json:%s\n", data)
	/*
		json:{"Title":"","Students":[{"Name":"stu00","Age":10,"Score":10},{"Name":"stu01","Age":10,"Score":10},{"Name":"stu02","Age":10,"Score":10},{"Name":"stu03","Age":10,"Score":10},{"Name":"stu04","Age":10,"Score":10},{"Name":"stu05","Age":10,"Score":10},{"Name":"stu06","Age":10,"Score":10},{"Name":"stu07","Age":10,"Score":10},{"Name":"stu08","Age":10,"Score":10},{"Name":"stu09","Age":10,"Score":10}]}
	*/

	t1 := Teacher{
		Name: "tom",
		Age:  18,
		ID:   100,
	}
	data, err = json.Marshal(t1)
	if err != nil {
		fmt.Println("json marshal failed")
		return
	}
	fmt.Printf("json:%s\n", data)
	// json:{"Name":"tom","Age":18,"ID":100}

	// map delete(map[string]int)
	m := make(map[string]Teacher)
	m["test1"] = Teacher{
		Name: "a",
		Age:  10,
		ID:   1,
	}
	m["test2"] = Teacher{
		Name: "b",
		Age:  10,
		ID:   2,
	}
	fmt.Println(m) // map[test1:{a 10 1} test2:{b 10 2}]

	delete(m, "test1")
	fmt.Println(m) // map[test2:{b 10 2}]
}
